﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Tools.Base
{
    public class XmlTool
    {
        public static string GetDefAttributeStr(XElement XML, string attribute, string strdef)
        {
            XAttribute attrib = XML.Attribute(attribute);
            if (null == attrib)
                return strdef;

            return (string)attrib;
        }

        public static int GetDefAttributeInt(XElement XML, string attribute, int dftValue)
        {
            XAttribute attrib = XML.Attribute(attribute);
            int result;
            if (attrib == null || string.IsNullOrEmpty((string)attrib))
            {
                result = dftValue;
            }
            else
            {
                result = Convert.ToInt32((string)attrib);
            }
            return result;
        }

        public static int GetDefAttributeInt(XElement XML, string root, string attribute, int dftValue)
        {
            int result = dftValue;
            XElement ele = XML.Element(root);
            if (ele != null)
            {
                XAttribute attrib = XML.Attribute(attribute);
                if (null != attrib)
                {
                    result = Convert.ToInt32((string)attrib);
                }
            }
            return result;
        }

        public static XAttribute GetSafeAttribute(XElement XML, string attribute)
        {
            XAttribute result;
            try
            {
                XAttribute attrib = XML.Attribute(attribute);
                if (null == attrib)
                {
                    throw new Exception(string.Format("读取属性: {0} 失败, xml节点名: {1}", attribute, GetXElementNodePath(XML)));
                }
                result = attrib;
            }
            catch (Exception)
            {
                throw new Exception(string.Format("读取属性: {0} 失败, xml节点名: {1}", attribute, GetXElementNodePath(XML)));
            }
            return result;
        }

        public static string GetSafeAttributeStr(XElement XML, string attribute)
        {
            XAttribute attrib = GetSafeAttribute(XML, attribute);
            return (string)attrib;
        }

        public static long GetSafeAttributeLong(XElement XML, string attribute, long def = -1L)
        {
            XAttribute attrib = GetSafeAttribute(XML, attribute);
            string str = (string)attrib;
            long result;
            if (string.IsNullOrEmpty(str.Trim()))
            {
                result = def;
            }
            else if (string.IsNullOrWhiteSpace(str))
            {
                result = def;
            }
            else
            {
                try
                {
                    result = (long)Convert.ToDouble(str);
                }
                catch (Exception)
                {
                    throw new Exception(string.Format("读取属性: {0} 失败, xml节点名: {1}", attribute, GetXElementNodePath(XML)));
                }
            }
            return result;
        }

        public static string GetXElementNodePath(XElement element)
        {
            string result;
            try
            {
                string path = element.Name.ToString();
                element = element.Parent;
                while (null != element)
                {
                    path = element.Name.ToString() + "/" + path;
                    element = element.Parent;
                }
                result = path;
            }
            catch (Exception)
            {
                result = "";
            }
            return result;
        }
    }
}
